package com.hzz.service.dsync;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.hzz.dao.eybase.IEyBaseRetrieveGmDao;
import com.hzz.dao.patrol.IPatrolTaskDao;
import com.hzz.dao.report.IReportEventDao;
import com.hzz.dao.river.IRiverInfoDao;
import com.hzz.dao.user.IUserDao;
import com.hzz.dto.dsync.PatrolTaskEventDSyncDto;
import com.hzz.dto.dsync.PatrolTaskRecordDSyncDto;
import com.hzz.entity.eybase.EyBaseRetrieveGm;
import com.hzz.entity.patrol.PatrolTask;
import com.hzz.entity.report.ReportEvent;
import com.hzz.entity.river.RiverInfo;
import com.hzz.framework.Response;
import com.hzz.framework.util.CommonConstant;
import com.hzz.framework.util.DateUtil;
import com.hzz.framework.util.StrUtil;
import com.hzz.framework.util.ValidatorUtil;
import com.hzz.task.DSyncTask;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

@Service
@Slf4j
public class PatrolTaskDSync extends BaseDSync {
    @Autowired
    IReportEventDao reportEventDao;
    @Autowired
    IPatrolTaskDao patrolTaskDao;
    @Autowired
    IRiverInfoDao riverInfoDao;
    @Autowired
    IEyBaseRetrieveGmDao eyBaseRetrieveGmDao;
    @Autowired
    IUserDao userDao;
//    @Autowired
//    IDSyncExistDao dsyncExistDao;

    /**
     * 巡河记录同步
     */
    public Response patrolTaskRecord(PatrolTask paramEntity) {
        log.info("patrolTaskRecord...");
        if (!isSyncEnabled()) {
            return Response.error("已停用同步功能");
        }
        Response response = Response.error("未执行");
        PatrolTask patrolTask = new PatrolTask();
        PatrolTaskRecordDSyncDto patrolTaskRecordDSyncDto = new PatrolTaskRecordDSyncDto();
        try {
            // 巡河记录代码	是
            patrolTaskRecordDSyncDto.setGuid(StrUtil.asString(paramEntity.getId()));
            patrolTask = (PatrolTask) patrolTaskDao.selectByPrimaryKey(paramEntity);
            if (patrolTask == null) {
                log.error("放弃同步PatrolTask:" + paramEntity.getId() + ",无任务");
                return response = Response.error("放弃同步" + paramEntity.getId() + ",无任务");
            }
            String taskStatus = patrolTask.getTaskStatus();
            if (!CommonConstant.TASK_STATUS_巡查任务_已巡查.equals(taskStatus)) {
                log.error("放弃同步PatrolTask:" + paramEntity.getId() + ",当前任务状态:" + taskStatus);
                return response = Response.error("放弃同步,当前任务状态:" + taskStatus);
            }
            if (ValidatorUtil.notEmpty(patrolTask.getRiverCourseCode())) {
                RiverInfo riverInfoEntity = new RiverInfo();
                riverInfoEntity.setId(new BigDecimal(patrolTask.getRiverCourseCode()).longValue());
                RiverInfo riverInfo = (RiverInfo) riverInfoDao.selectByPrimaryKey(riverInfoEntity);
                if (riverInfo == null) {
                    log.error("放弃同步:" + patrolTask.getId() + ",巡查对象获取失败!,riverInfo.id=" + riverInfoEntity.getId());
                    return response = Response.error("放弃同步" + patrolTask.getId() + ",巡查对象获取失败!,riverInfo.id=" + riverInfoEntity.getId());
                }
                // 上级系统id = 本系统id去除最高位[11110000,22220000,33330000]
                Long riverId = riverInfo.getId();
                String riverIdStr = ("" + riverInfo.getId());
                if (riverIdStr.length() > 8 && (riverIdStr.startsWith("11110000") || riverIdStr.startsWith("22220000") || riverIdStr.startsWith("33330000"))) {
                    riverId = Long.parseLong(riverIdStr.substring(8));
                }
                Object userId = userDao.findUserIdByAccount(patrolTask.getExecutorAccount());
                EyBaseRetrieveGm gmEntity = new EyBaseRetrieveGm();
                gmEntity.setId(StrUtil.asString(userId));
                EyBaseRetrieveGm userGM = (EyBaseRetrieveGm) eyBaseRetrieveGmDao.selectByPrimaryKey(gmEntity);
                if (userGM == null) {
                    log.error("放弃同步PatrolTask:" + patrolTask.getId() + ",当前任务无省平台河长信息");
                    return response = Response.error("放弃同步" + patrolTask.getId() + ",当前任务无省平台河长信息!");
                }
                // 河长代码	是
                patrolTaskRecordDSyncDto.setGmCode(userGM.getId());
                // 河长姓名	是
                patrolTaskRecordDSyncDto.setRiverChief(userGM.getName());
//                    // 河长职务	是
//                    patrolTaskRecordDSyncDto.setRiverChiefPost(user.getJob());
                // 行政区代码	是
                patrolTaskRecordDSyncDto.setPac(riverInfo.getPac());
                if (patrolTaskRecordDSyncDto.getPac() == null) {
                    patrolTaskRecordDSyncDto.setPac("511025");
                }
                // 河长级别	否	1:省级；02：厅局级；03：县处级；04：乡镇级；05：村级
                String level = "03";
                if (ValidatorUtil.notEmpty(userGM.getAdRank())) {
                    if (userGM.getAdRank().indexOf("省") != -1) {
                        level = "01";
                    } else if (userGM.getAdRank().indexOf("厅局") != -1) {
                        level = "02";
                    } else if (userGM.getAdRank().indexOf("乡镇") != -1) {
                        level = "04";
                    } else if (userGM.getAdRank().indexOf("村") != -1) {
                        level = "05";
                    } else {
                        level = "03";
                    }
                }
                patrolTaskRecordDSyncDto.setRiverchiefLevel(level);
                // 巡查对象类型 是 111:河段;121:渠段; 221:湖片;231:水库片; 251:湿地;
                // 巡查对象类型	是	111:河段;121:渠段; 221:湖片;231:水库片; 251:湿地(entity.get());
                String objType = "111";
                String riverbasinName = riverInfo.getHnnm();
                // 巡查对象代码	是
                String sid = "" + riverId;
                // 河段id	是
                Long toolObjectId = riverId;
                // 河流id	是
                Long toolClassSign = riverId;
                switch (riverInfo.getType()) { // 水域类型(1河流,2水库,3渠段)
                    case 1:
                        objType = "111";
                        if (!ValidatorUtil.notEmpty(riverbasinName)) {
                            riverbasinName = riverInfo.getName();
                        }
                        break;
                    case 2:
                        objType = "231";
                        if (!ValidatorUtil.notEmpty(riverbasinName)) {
                            riverbasinName = riverInfo.getHnnm();
                        }
                        break;
                    case 3:
                        objType = "121";
                        if (!ValidatorUtil.notEmpty(riverbasinName)) {
                            riverbasinName = riverInfo.getHnnm();
                        }
                        break;
                }
                patrolTaskRecordDSyncDto.setObjType(objType);

                // 流域	是	01嘉陵江；02涪江；03长江（金沙江）；04沱江；05大渡河；06岷江；07渠江；08汉江；09雅砻江；10黄河；11青衣江；12安宁河
                String riverbasin = "04";
                if (riverbasinName != null) {
                    if (riverbasinName.indexOf("嘉陵") != -1) {
                        riverbasin = "01";
                    } else if (riverbasinName.indexOf("涪江") != -1) {
                        riverbasin = "02";
                    } else if (riverbasinName.indexOf("长江") != -1 || riverbasinName.indexOf("金沙") != -1) {
                        riverbasin = "03";
                    } else if (riverbasinName.indexOf("沱江") != -1) {
                        riverbasin = "04";
                    } else if (riverbasinName.indexOf("大渡") != -1) {
                        riverbasin = "05";
                    } else if (riverbasinName.indexOf("岷江") != -1) {
                        riverbasin = "06";
                    } else if (riverbasinName.indexOf("渠江") != -1) {
                        riverbasin = "07";
                    } else if (riverbasinName.indexOf("汉江") != -1) {
                        riverbasin = "08";
                    } else if (riverbasinName.indexOf("雅砻") != -1) {
                        riverbasin = "09";
                    } else if (riverbasinName.indexOf("黄河") != -1) {
                        riverbasin = "10";
                    } else if (riverbasinName.indexOf("青衣") != -1) {
                        riverbasin = "11";
                    } else if (riverbasinName.indexOf("安宁") != -1) {
                        riverbasin = "12";
                    }
                }
                patrolTaskRecordDSyncDto.setRiverbasin(riverbasin);
                // 巡查对象名称	是
                patrolTaskRecordDSyncDto.setRiver(riverInfo.getName());
                // 巡查对象分段名称	是
                patrolTaskRecordDSyncDto.setRiverSection(riverInfo.getName());
                // 巡查对象代码	是
                patrolTaskRecordDSyncDto.setSid(sid);
                // 河段id	是
                patrolTaskRecordDSyncDto.setToolObjectId(toolObjectId);
                // 河流id	是
                patrolTaskRecordDSyncDto.setToolClassSign(toolClassSign);
//                // 河段名称	是 - 内江接口 无这字段
//                patrolTaskRecordDSyncDto.setToolObjectName(riverInfo.getName());
                // 河流名称不能为空	是
                patrolTaskRecordDSyncDto.setRiverBasinName(riverInfo.getName());
            }
            // 巡河开始时间	是
            if (patrolTask.getExcStartTime() != null) {
                patrolTaskRecordDSyncDto.setPatrolRiverStartDate(DateUtil.getDateTimeStr(patrolTask.getExcStartTime()));
            } else {
                patrolTaskRecordDSyncDto.setPatrolRiverStartDate(DateUtil.getDateTimeStr(patrolTask.getStartTime()));
            }
            // 巡河结束时间	是
            if (patrolTask.getExcEndTime() != null) {
                patrolTaskRecordDSyncDto.setPatrolRiverEndDate(DateUtil.getDateTimeStr(patrolTask.getExcEndTime()));
            } else {
                patrolTaskRecordDSyncDto.setPatrolRiverEndDate(DateUtil.getDateTimeStr(patrolTask.getEndTime()));
            }
            // 主要巡河人员	否
            patrolTaskRecordDSyncDto.setAccompanyPeople(patrolTask.getExecutorName());
            // 巡河人员数	否
            //   patrolTaskRecordDSyncDto.setPatrolRiverPeopleNum("1");
            // 主要参加单位	否
            // patrolTaskRecordDSyncDto.setParticipateUnit(entity.get());
            // 参加单位数	否
            // patrolTaskRecordDSyncDto.setParticipateUnitNum(entity.get());
            // 巡河会议名称	否
            // patrolTaskRecordDSyncDto.setPatrolRiverMeetingName(entity.get());
            // 巡河会议内容	否
            patrolTaskRecordDSyncDto.setPatrolRiverMeetingInfo(patrolTask.getTaskContent());
            // 巡河开始地点	否
            // patrolTaskRecordDSyncDto.setStartLoc(entity.get());
            // 巡河结束地点	否
            // patrolTaskRecordDSyncDto.setEndLoc(entity.get());
            // 巡查时长(小时)	否
            // patrolTaskRecordDSyncDto.setTourduration(entity.get());
            // 巡查长度(千米)	否
            if (ValidatorUtil.notEmpty(patrolTask.getPatrolKilometer())) {
                patrolTaskRecordDSyncDto.setTourLen(new BigDecimal(patrolTask.getPatrolKilometer()).doubleValue());
            }
            if (ValidatorUtil.notEmpty(patrolTask.getPatrolTrack())) {
                // 巡查轨迹（轨迹坐标点集合）([{lon,lat},{lon,lat}...]) ->
                // object	巡查轨迹	否	[{"Lng":"0000","Lat":"000","Address":"地址","TrackTime":"2020-12-12"}]
                patrolTaskRecordDSyncDto.setTrackPoints(JSONArray.parseArray(StrUtil.replaceAll(patrolTask.getPatrolTrack(), "lon", "lng")));
            }
            // 备注	否
            patrolTaskRecordDSyncDto.setNote(patrolTask.getMemo());
//            // 巡河记录附件 否
//            patrolTaskRecordDSyncDto.setConfirmFile();
//            // 异常数量记录 否
//            Integer ExceptionTotalCount;
//            // 巡河确认小图	否
//            String MiniConfirmFile;
            response = syncPost(CommonConstant.TASK_推送_巡查任务, JSON.toJSONString(patrolTaskRecordDSyncDto), patrolTaskRecord);
            log.info("巡河记录同步{}", response);
            if (response.code == CommonConstant.TASK_STATUS_基础数据_成功拉取 || response.getMessage().indexOf("已存在") != -1) {
                // 检索任务事件.
                ReportEvent reportEvent = new ReportEvent();
                reportEvent.setPatrolTaskId(patrolTask.getId());
                reportEvent.setReportingType(0);
                List<ReportEvent> reportEvents = (List<ReportEvent>) reportEventDao.findDataIsList(reportEvent);
                if (reportEvents != null && reportEvents.size() > 0) {
                    for (ReportEvent dSync : reportEvents) {
                        if (dSync.getPatrolTaskId() == null/* || dsyncExistDao.isDataFinishById(StrUtil.asString(dSync.getId())) > 0*/) {
                            continue;
                        }
                        Response eventResponse = null;
                        try {
                            eventResponse = patrolTaskEvent(dSync);
                        } catch (Exception e) {
                            eventResponse = Response.error(500);
                            log.error("同步失败!", e);
                        } finally {
                            log.warn("TaskEvent同步结果:{}", eventResponse);
                        }
                    }
                }
            } else {
                log.error("巡河记录同步\n反馈{}\n数据{}", response, JSON.toJSONString(patrolTaskRecordDSyncDto));
            }
        } catch (Exception e) {
            log.error("巡河记录同步失败!" + JSON.toJSONString(patrolTaskRecordDSyncDto));
            log.error("巡河记录同步失败!", e);
            response = Response.error(e.getMessage());
        } finally {
            logDSyncExist(patrolTaskRecordDSyncDto.getGuid(), CommonConstant.TASK_推送_巡查任务, JSON.toJSONString(patrolTaskRecordDSyncDto), response, CommonConstant.TASK_STATUS_巡查任务_已巡查.equals(patrolTask.getTaskStatus()));
        }
//            updateState(nextId, CommonConstant.TASK_推送_巡查任务patrolTaskRecord", patrolTaskRecordDSyncDto);
        return response;
    }

    /**
     * 巡河事件同步
     */
    public Response patrolTaskEvent(ReportEvent paramEntity) {
        log.debug("patrolTaskEvent...");
        if (!isSyncEnabled()) {
            return Response.error("已停用同步功能");
        }
        Response response = Response.error("未执行");
        PatrolTaskEventDSyncDto patrolTaskEventDSyncDto = new PatrolTaskEventDSyncDto();
        try {
            // 巡河事件代码 是
            patrolTaskEventDSyncDto.setEvCode(StrUtil.asString(paramEntity.getId()));
            ReportEvent entity = (ReportEvent) reportEventDao.selectByPrimaryKey(paramEntity);
            if (entity == null || entity.getPatrolTaskId() == null) {
                log.error("放弃同步ReportEvent:" + paramEntity.getId() + ",无父级任务id");
                return response = Response.error("无父级任务id");
            }
            // 巡河记录代码 是
            patrolTaskEventDSyncDto.setTourGuid(StrUtil.asString(entity.getPatrolTaskId()));

            PatrolTask patrolTaskEntiry = new PatrolTask();
            patrolTaskEntiry.setId(entity.getPatrolTaskId());
            PatrolTask patrolTask = (PatrolTask) patrolTaskDao.selectByPrimaryKey(patrolTaskEntiry);
            if (patrolTask == null) {
                log.error("放弃同步ReportEvent:" + entity.getId() + ",无父级任务");
                return response = Response.error("无父级任务");
            }
            String taskStatus = patrolTask.getTaskStatus();
            if (!CommonConstant.TASK_STATUS_巡查任务_已巡查.equals(taskStatus)) {
                log.error("放弃同步ReportEvent:" + entity.getId() + ",当前任务状态:" + taskStatus);
                return response = Response.error("放弃同步,当前任务状态:" + taskStatus);
            }
            RiverInfo riverInfoEntity = new RiverInfo();
            riverInfoEntity.setId(new BigDecimal(patrolTask.getRiverCourseCode()).longValue());
            RiverInfo riverInfo = (RiverInfo) riverInfoDao.selectByPrimaryKey(riverInfoEntity);
            if (riverInfo == null) {
                log.error("放弃同步:" + patrolTask.getId() + ",巡查对象获取失败!,riverInfo.id=" + riverInfoEntity.getId());
                return response = Response.error("放弃同步" + patrolTask.getId() + ",巡查对象获取失败!,riverInfo.id=" + riverInfoEntity.getId());
            }
            EyBaseRetrieveGm gmEntity = new EyBaseRetrieveGm();
            gmEntity.setId(patrolTask.getExecutorAccount());
            EyBaseRetrieveGm userGM = (EyBaseRetrieveGm) eyBaseRetrieveGmDao.selectByPrimaryKey(gmEntity);
            if (userGM == null) {
                userGM = new EyBaseRetrieveGm();
            }
            // 问题发生行政区划码 是
            patrolTaskEventDSyncDto.setAdCode(riverInfo.getPac());
            if (ValidatorUtil.notEmpty(entity.getEventType())) {
                String evtype = "9";
                String evcategoryType = "990";
                switch (entity.getEventType()) {
                    case "河岸倾倒垃圾": {
                        evtype = "3";
                        evcategoryType = "311";
                        break;
                    }
                    case "生活非法排污": {
                        evcategoryType = "911";
                        break;
                    }
                    case "工业区非法排污": {
                        evcategoryType = "912";
                        break;
                    }
                    case "非法采砂": {
                        evtype = "2";
                        evcategoryType = "211";
                        break;
                    }
                    case "河道及禁养区非法养殖": {
                        evtype = "4";
                        evcategoryType = "991";
                        break;
                    }
                    case "河湖岸线乱搭乱建": {
                        evtype = "4";
                        evcategoryType = "421";
                        break;
                    }
                    case "电鱼毒鱼": {
                        evcategoryType = "921";
                        break;
                    }
                    case "饮用水源地污染": {
                        evcategoryType = "992";
                        break;
                    }
                    case "河面漂浮物及垃圾": {
                        evcategoryType = "391";
                        break;
                    }
                    case "面源污染": {
                        evcategoryType = "993";
                        break;
                    }
                    case "重金属污染": {
                        evcategoryType = "994";
                        break;
                    }
                    case "场镇污水处理厂未建设": {
                        evcategoryType = "995";
                        break;
                    }
                    case "场镇污水处理厂未运行": {
                        evcategoryType = "996";
                        break;
                    }
                    case "场镇污水处理厂处理不达标": {
                        evcategoryType = "997";
                        break;
                    }
                    case "场镇污水管网收集率不高": {
                        evcategoryType = "998";
                        break;
                    }
                    case "电站截留生态流量": {
                        evcategoryType = "999";
                        break;
                    }
                }
                // 事件分类 是 2.乱采 3.乱堆 4.乱建 9.其他
                patrolTaskEventDSyncDto.setEvType(evtype);
                // * 事件名称 否
                String evTypeName = "";
                switch (evtype) {
                    case "2":
                        evTypeName = "乱采";
                        break;
                    case "3":
                        evTypeName = "乱堆";
                        break;
                    case "4":
                        evTypeName = "乱建";
                        break;
                    default:
                        evTypeName = "其他";
                }
                patrolTaskEventDSyncDto.setEvTypeName(evTypeName);
                // 事件子类 是 211非法采砂;311河岸倾倒垃圾; 391河面漂浮物及垃圾; 421河湖岸线乱搭乱建;911生活非法排污; 912工业区非法排污; 921电鱼、毒鱼; 991河道及禁养区非法养殖; 992饮用水源地污染;993面源污染;994重金属污染;995场镇污水处理厂未建设;996场镇污水处理厂未运行;997场镇污水处理厂处理不达标;998场镇污水管网收集率不高;999电站截留生态流量; 990其他问题;
                patrolTaskEventDSyncDto.setEvCategory(evcategoryType);
                // 事件子类名称
                String evCategoryName = "";
                switch (evcategoryType) {
                    case "211":
                        evCategoryName = "非法采砂";
                        break;
                    case "311":
                        evCategoryName = "河岸倾倒垃圾";
                        break;
                    case "391":
                        evCategoryName = "河面漂浮物及垃圾";
                        break;
                    case "421":
                        evCategoryName = "河湖岸线乱搭乱建";
                        break;
                    case "911":
                        evCategoryName = "生活非法排污";
                        break;
                    case "912":
                        evCategoryName = "工业区非法排污";
                        break;
                    case "921":
                        evCategoryName = "电鱼、毒鱼";
                        break;
                    case "991":
                        evCategoryName = "河道及禁养区非法养殖";
                        break;
                    case "992":
                        evCategoryName = "饮用水源地污染";
                        break;
                    case "993":
                        evCategoryName = "面源污染";
                        break;
                    case "994":
                        evCategoryName = "重金属污染";
                        break;
                    case "995":
                        evCategoryName = "场镇污水处理厂未建设";
                        break;
                    case "996":
                        evCategoryName = "场镇污水处理厂未运行";
                        break;
                    case "997":
                        evCategoryName = "场镇污水处理厂处理不达标";
                        break;
                    case "998":
                        evCategoryName = "场镇污水管网收集率不高";
                        break;
                    case "999":
                        evCategoryName = "电站截留生态流量";
                        break;
                    case "990":
                        evCategoryName = "其他问题";
                        break;
                    default:
                        evCategoryName = "其他问题";
                }
                patrolTaskEventDSyncDto.setEvCategoryName(evCategoryName);
            }
//                patrolTaskEventDSyncDto.setBegetts(DateUtil.getDateTimeStr(entity.getReportingDate()));// 问题产生时间 否
            if (entity.getReportingDate() != null) {
                patrolTaskEventDSyncDto.setFindts(DateUtil.getDateTimeStr(entity.getReportingDate()));// 问题发现时间 是
            }
            patrolTaskEventDSyncDto.setLoc(entity.getRiverName());// 发生位置描述 否
            patrolTaskEventDSyncDto.setEvDesc(entity.getEventInfo());// 问题描述 否
            String evLevel = "3";
            if (entity.getEventType() != null) {
                if (entity.getEventType().indexOf("垃圾") != -1 || entity.getEventType().indexOf("乱建") != -1) {
                    evLevel = "1";
                } else if (entity.getEventType().indexOf("排污") != -1) {
                    evLevel = "2";
                }
            }
            patrolTaskEventDSyncDto.setEvLevel(evLevel);// " 事件等级,也叫严重程度 否 1重大、2严重、3一般
            patrolTaskEventDSyncDto.setSourceType("1");// 事件来源 是 1日常巡河；
            patrolTaskEventDSyncDto.setBrandInfo("1");// 河长公示牌上的河长信息是否完整正确 否 1是、0否、9无
            if (entity.getDateDeadline() != null) {
                patrolTaskEventDSyncDto.setPlants(DateUtil.getDateTimeStr(entity.getDateDeadline()));// 计划完成时间 否
            }
            patrolTaskEventDSyncDto.setDataType("1");// 数据来源 是 1系统填报
            patrolTaskEventDSyncDto.setDealDesc(entity.getTreatmentMemo());// 问题处理建议 否
            // 事件进度(0上报,1处理,2结案)
            String status = "15";
            if (entity.getState() == 2) {
                status = "91";
            } else {
                // 事件处理方式:0直接处理,1分配处理,2报河长办处理,3报上级河长处理
                switch (entity.getTreatmentType()) {
                    case 0: {
                        status = "11";
                        break;
                    }
                    case 1: {
                        status = "13";
                        break;
                    }
                    case 2: {
                        status = "14";
                        break;
                    }
                    case 3: {
                        status = "12";
                        break;
                    }

                }
            }
            patrolTaskEventDSyncDto.setStatus(status);// 事件处理状态 是 01.未整改； 11.整改中；12.提交上级河长处理；13.交下级部门处理.14.交有关职能部门处理.15.其它；91.已整改；
            patrolTaskEventDSyncDto.setStatuSprocess(entity.getResultFeedback());// 事件当前处理进程 是
            if (entity.getTreatmentDate() != null) {
                patrolTaskEventDSyncDto.setDoneDate(DateUtil.getDateTimeStr(entity.getTreatmentDate()));// 完成时间 是
            }
            patrolTaskEventDSyncDto.setRepoName(entity.getReportingUserName());// 上报人姓名 否
            patrolTaskEventDSyncDto.setMobile(entity.getReportingUserCellphone());// 上报人联系电话 否
            patrolTaskEventDSyncDto.setRepoComp(entity.getDutyOrgName());// 上报人单位 否
//            patrolTaskEventDSyncDto.setShape(entity.get())  ;// 多个坐标点 否
            patrolTaskEventDSyncDto.setPhotoUrl(entity.getTreatmentPrePics());// 图片各自的唯一网络地址文件名 否
            if (ValidatorUtil.notEmpty(entity.getLatitude())) {
                patrolTaskEventDSyncDto.setLat(new BigDecimal(entity.getLatitude()));// 维度 否
            }
            if (ValidatorUtil.notEmpty(entity.getLongitude())) {
                patrolTaskEventDSyncDto.setLng(new BigDecimal(entity.getLongitude()));// 经度 否
            }
            response = syncPost(CommonConstant.TASK_推送_巡查事件, JSON.toJSONString(patrolTaskEventDSyncDto), patrolTaskEvent);
            log.info("巡河事件同步{}", response);
            if (!(response.code == CommonConstant.TASK_STATUS_基础数据_成功拉取 || response.getMessage().indexOf("已存在") != -1)) {
                log.error("巡河事件同步\n反馈{}\n数据{}", response, JSON.toJSONString(patrolTaskEventDSyncDto));
            }
        } catch (Exception e) {
            log.error("巡河事件同步失败!" + JSON.toJSONString(patrolTaskEventDSyncDto));
            log.error("巡河事件同步失败!", e);
            response = Response.error(e.getMessage());
        } finally {
            logDSyncExist(patrolTaskEventDSyncDto.getEvCode(), CommonConstant.TASK_推送_巡查事件, JSON.toJSONString(patrolTaskEventDSyncDto), response, true);
        }
        return response;
    }

    public void logDSyncExist(String nextId, String type, String djson, Response result, Boolean finishState) {
        try {
            Integer dstate = ((result.getCode() == CommonConstant.TASK_STATUS_基础数据_成功拉取 && finishState) ? 1 : 2);
            if (dstate != 1 && result != null && result.getMessage() != null && result.getMessage().indexOf("已存在") != -1) {
                dstate = 1;
            }
            if (CommonConstant.TASK_推送_巡查任务.equalsIgnoreCase(type)) {
                PatrolTask patrolTask = new PatrolTask();
                patrolTask.setId(Long.parseLong(nextId));
                patrolTask.setDsyncState(dstate);
                patrolTask.setDsyncDjson(djson);
                patrolTask.setDsyncResult(JSON.toJSONString(result));
                patrolTaskDao.updateDSync(patrolTask);
            } else if (CommonConstant.TASK_推送_巡查事件.equalsIgnoreCase(type)) {
                ReportEvent reportEvent = new ReportEvent();
                reportEvent.setId(Long.parseLong(nextId));
                reportEvent.setDsyncState(dstate);
                reportEvent.setDsyncDjson(djson);
                reportEvent.setDsyncResult(JSON.toJSONString(result));
                reportEventDao.updateDSync(reportEvent);
            }
        } catch (Exception e) {
            log.error("更新同步日志,异常!", e);
        }
    }

    public static void main(String[] args) {
        long s = 19890102;
        System.out.println(Long.parseLong(("" + s).substring(1)));
    }
}
